﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XmlDemo
{
    public partial class XmlWrite : Form
    {
        private XmlNode xmlNode;
        public XmlNode XmlNode
        {
            get { return xmlNode; }
            set { xmlNode = value; }
        }
        private XmlDocument myXml;
        public XmlDocument MyXml
        {
            get { return myXml; }
            set { myXml = value; }
        }
        public XmlWrite()
        {
            InitializeComponent();
            this.treeView1.LabelEdit = true;
            this.treeView1.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseDoubleClick);
            this.treeView1.AfterLabelEdit += new NodeLabelEditEventHandler(treeView1_AfterLabelEdit);
            this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
            this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
        }

        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeView1.SelectedNode == this.treeView1.TopNode)
            {
                this.btnUp.Enabled = false;
                this.btnDown.Enabled = false;
                return;
            }
            if (this.treeView1.SelectedNode.Parent.Nodes.Count == 1)
            {
                this.btnUp.Enabled = false;
                this.btnDown.Enabled = false;
                return;
            }
            this.btnUp.Enabled = true;
            this.btnDown.Enabled = true;
        }

        void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
        }
        void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            try
            {
                string oldText = e.Node.Text;
                if (e.Label.Length == 0)
                {
                    e.Node.EndEdit(false);
                }
                XmlNode xmlNod = e.Node.Tag as XmlNode;
                XmlElement xe = xmlNod as XmlElement;
                xe.SetAttribute("name", e.Label);
                e.Node.Text = e.Label;
                e.Node.EndEdit(true);
            }
            catch (Exception ex){}
        }
        void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node == this.treeView1.TopNode) return;
            e.Node.BeginEdit();
        }
        private void BindDataToTree()
        {
            this.treeView1.Nodes.Clear();
            TreeNode tn=new TreeNode ();
            tn.Text = "全部";
            tn.Tag = myXml.SelectSingleNode("address");
            createNode(myXml.SelectSingleNode("address"), tn);
            this.treeView1.Nodes.Add(tn);
        }
        private void createNode(XmlNode xmlNode, TreeNode trn)
        {
            TreeNode tn =null;
            foreach (XmlNode xmlNode0 in xmlNode.ChildNodes)
            {
                XmlElement xe = xmlNode0 as XmlElement;
                tn = new TreeNode();
                tn.Text = xe.GetAttribute("name").ToString();
                tn.Tag = xmlNode0;
                if (xmlNode0.ChildNodes.Count != 0)
                {
                    createNode(xmlNode0, tn);
                }
                trn.Nodes.Add(tn);
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode currntNode = this.treeView1.SelectedNode; 
            XmlNode currnXmlNode = currntNode.Tag as XmlNode;// myXml.SelectSingleNode("*/province[@name='" + this.cmbProvince.Text + "']");
            if (currnXmlNode.Name == "address") return;
            string exname = string.Empty;
            if (currnXmlNode == null)
            {
               exname ="province";
            }
            else
            {
                if (currnXmlNode.Name == "province")
                {
                    exname = "city";
                }
                else if (currnXmlNode.Name == "city")
                {
                    exname = "country";
                }
            }
            TreeNode newTreeNode = new TreeNode();
            newTreeNode.Text = "新建..";
            currntNode.Nodes.Add(newTreeNode);
            XmlElement newChild = myXml.CreateElement(exname);
            newChild.SetAttribute("name", newTreeNode.Text);
            currnXmlNode.AppendChild(newChild);
            newTreeNode.Tag = newChild;
            treeView1.SelectedNode = newTreeNode;
            newTreeNode.BeginEdit();
            this.treeView1.Select();
        }
        private void XmlWrite_Load(object sender, EventArgs e)
        {

            BindDataToTree();
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            myXml.Save("Area.xml");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            XmlNode xmln = this.treeView1.SelectedNode.Tag as XmlNode;
            if (xmln != null)
            {
                xmln.ParentNode.RemoveChild(xmln);
            }
            this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
          moveTreeNode(true);
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            moveTreeNode(false );
        }
        private void moveTreeNode(bool upDown)
        {
            TreeNode curTreeNode = this.treeView1.SelectedNode;
            int upNodeIndex = curTreeNode.Index;
            TreeNode parTreeNode = curTreeNode.Parent;
            TreeNode cloneTreeNode = (TreeNode)(curTreeNode.Clone());
            this.treeView1.Nodes.Remove(curTreeNode);
            XmlNode preXmlNode = null;
            if (upDown)
            {
                parTreeNode.Nodes.Insert(upNodeIndex - 1, cloneTreeNode);
                preXmlNode = cloneTreeNode.NextNode.Tag as XmlNode;
            }
            else
            {
                parTreeNode.Nodes.Insert(upNodeIndex + 1, cloneTreeNode);
                preXmlNode = cloneTreeNode.PrevNode.Tag as XmlNode;
            }
            

            XmlNode currXmlNode = cloneTreeNode.Tag as XmlNode;
            XmlNode parXmlNode = parTreeNode.Tag as XmlNode;
            XmlNode xmlCone = currXmlNode.CloneNode(true);
            parXmlNode.RemoveChild(currXmlNode);
            if (upDown)
            {
                parXmlNode.InsertBefore(xmlCone, preXmlNode);
            }
            else
            {
                parXmlNode.InsertAfter(xmlCone, preXmlNode);
            }

            this.treeView1.SelectedNode = cloneTreeNode;
            this.treeView1.SelectedNode.Tag = xmlCone;
            this.treeView1.SelectedNode.BackColor = Color.CadetBlue;
            this.treeView1.SelectedNode.EndEdit(true);
        }
    }
}
